//
//  HYDiscoverSearchView.swift
//  WeiBo
//
//  Created by crazyfish on 16/3/11.
//  Copyright © 2016年 crazyfish. All rights reserved.
//

import UIKit

class HYDiscoverSearchView: UIView {
    
  
 
  @IBOutlet weak var cancelBtn: UIButton!
  @IBOutlet weak var TextField: UITextField!
    //textfield右边的约束
  @IBOutlet weak var TextFieldRight: NSLayoutConstraint!
    
    override func awakeFromNib() {
        
        //这里设置leftView是可以的
        //因为在这个方法里面textfield是有值的(initwithcode方法执行的时候textfield还没有连好线)
        
        leftView.frame.size = CGSizeMake(self.frame.height, self.frame.height)
        
        TextField.leftView = leftView
        
        //设置左侧view的模式为总是显示(默认是不显示的)
        TextField.leftViewMode = .Always
        
        
    }
    
    @IBAction func EditingDidBegin(sender: AnyObject) {
        
        //开始编辑的时候弹出键盘,显示出取消按钮
        TextFieldRight.constant = cancelBtn.frame.width
        
        UIView.animateWithDuration(0.25) { () -> Void in
            
            //执行约束动画要把layoutifneeds放到动画的block中才会有效果
            self.layoutIfNeeded()
        }
        
    }
    
    @IBAction func cancelBtnClik(sender: AnyObject) {
        
        //点击取消按钮要退出键盘,把cancel按钮隐藏
        
        //取消第一响应者
        TextField.resignFirstResponder()
        
        //把textField右边的约束修改成0
        TextFieldRight.constant = 0
        
        UIView.animateWithDuration(0.25) { () -> Void in
            
            self.layoutIfNeeded()
        }
        
    }
    
    class func searchView()->HYDiscoverSearchView{
        
        //加载xib
        
        return NSBundle.mainBundle().loadNibNamed("HYDiscovery", owner: nil, options: nil).last as! HYDiscoverSearchView
        
    }
    
    //懒加载放大镜控件
    
    private lazy var leftView: UIImageView = {
    
     let leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        
      //让leftView中的图片居中放大,图片框.图片大小不变
      leftView.contentMode = .Center
        
      return leftView
    
    }()
    
    

}
